﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MonitorPlan.Admin
{
    public partial class Tool : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["userid"] == null)
                {
                    Response.Redirect("/Admin");
                }
            }
        }

        protected void rp1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            // delete a tool when you user click on delete button in repeater.
            if (e.CommandName == "delete")
            {
                string id = e.CommandArgument.ToString();
                Model.Tool t = new Model.Tool();
                if (t.Remove(id))
                {
                    this.rp1.DataBind();
                    this.mv.ActiveViewIndex = 0;
                }
            }
            // view room in detail
        }

        protected void btnView_Click(object sender, EventArgs e)
        {
            this.rp1.DataBind();
            this.mv.ActiveViewIndex = 0;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            this.mv.ActiveViewIndex = 1;
            this.txtToolDesc.Text = "";
            this.txtToolName.Text = "";
            this.txtKeyOff.Text = "";
            this.txtKeyOn.Text = "";
            this.actionSms.InnerHtml = "";
            this.txtToolName.Focus();
        }

        protected void ddlLocationName_SelectedIndexChanged(object sender, EventArgs e)
        {
            dsHouse.SelectCommand = "select houseid, housename from house where isdeleted=0 and locationid=" + this.ddlLocationName.SelectedValue;
            ddlHouseName.DataBind();
            //dsRoom.SelectCommand = "select roomid, roomname from room where isdeleted=0 and houseid=" + this.ddlHouseName.SelectedValue;
            //ddlRoomName.DataBind();
        }

        protected void ddlHouseName_SelectedIndexChanged(object sender, EventArgs e)
        {
            //dsRoom.SelectCommand = "select roomid, roomname from room where isdeleted=0 and houseid=" + this.ddlHouseName.SelectedValue;
            //ddlRoomName.DataBind();
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            this.mv.ActiveViewIndex = 0;
            this.rp1.DataBind();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.txtToolDesc.Text = "";
            this.txtToolName.Text = "";
            this.txtKeyOff.Text = "";
            this.txtKeyOn.Text = "";
            this.actionSms.InnerHtml = "";
            this.txtToolName.Focus();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string sms;
            this.actionSms.InnerHtml = "Test";
            Model.Tool t = new Model.Tool();
            t.Id = this.ddlHouseName.SelectedValue + this.txtKeyOn.Text + this.txtKeyOff.Text;
            t.Name = this.txtToolName.Text;
            t.ToolType = this.ddlType.SelectedValue;
            t.KeyOn = this.txtKeyOn.Text;
            t.KeyOff = this.txtKeyOff.Text;
            t.HouseId = Convert.ToInt32(this.ddlHouseName.SelectedValue);
            t.Description = this.txtToolDesc.Text;
           // t.RoomId = Convert.ToInt32(this.ddlRoomName.SelectedValue);
            if(t.KeyOff!="" && t.KeyOn!="")
            {
               if(t.CheckKey())
               {
                   if (t.Add())
                   {
                       sms = "<div class='alert alert-success'>";
                       sms += "<button type='button' class='close' data-dismiss='alert'>&times;</button>";
                       sms += "Data has been save successfully!";
                       sms += "</div>";
                       this.txtToolDesc.Text = "";
                       this.txtToolName.Text = "";
                       this.txtKeyOff.Text = "";
                       this.txtKeyOn.Text = "";
                       this.txtToolName.Focus();
                       this.actionSms.InnerHtml = sms;
                   }
                   else
                   {
                       sms = "<div class='alert alert-error'>";
                       sms += "<button type='button' class='close' data-dismiss='alert'>&times;</button>";
                       sms += "Data has not been saved successfully. Try again!";
                       sms += "</div>";
                       this.actionSms.InnerHtml = sms;
                   }
               }
               else
               {
                   this.actionSms.InnerHtml = "<span style='color:red'>The key is already used in this house. Try another one!</span>";
               }
            }
            else
            {
                this.actionSms.InnerHtml = "<span style='color:red'>Please input keyon and keyoff!</span>";
            }
        }
    }
}